﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZTool.Domain.DO;

namespace ZTool.Domain
{
    /// <summary>
    /// 注册中心
    /// </summary>
    class RegistryCenter
    {
        private ConcurrentDictionary<string, Server> registeredServices = new ConcurrentDictionary<string, Server>();
        public ConcurrentDictionary<string, Server> Services => registeredServices;

        public void Register(string serviceName, Server server)
        {
            registeredServices[serviceName] = server;
        }
        public void UpdateTime(string serviceName, TimeDate time)
        {
            if (!string.IsNullOrEmpty(serviceName) && registeredServices.ContainsKey(serviceName))
            {
                var service = registeredServices[serviceName];

                service.SetUpdateTime(time);
                Console.WriteLine($"Heartbeat received for service '{serviceName}'.");
            }
        }

        public bool Contain(string name)
        {
            return registeredServices.ContainsKey(name);
        }
    }
}
